package com.greenantmall.adapter;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.List;

import com.avos.avoscloud.AVUser;
import com.greenantmall.R;
import com.greenantmall.AntHomeApplication;
import com.greenantmall.activity.OrderDetailActivity;
import com.greenantmall.activity.fragment.OrdersListFragment;
import com.greenantmall.bean.MyMsg;
import com.greenantmall.bean.Order;
import com.greenantmall.bean.OrderItem;
import com.greenantmall.bean.StoreInfo;
import com.greenantmall.service.RetrieveDataAsyn;
import com.greenantmall.service.RetrieveDataListener;
import com.greenantmall.utils.Const;
import com.greenantmall.utils.NotificationUtil;
import com.greenantmall.utils.UtilTools;
import com.greenantmall.utils.iLog;
import com.greenantmall.view.CommonDialog;
import com.lidroid.xutils.BitmapUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MyOrderListAdapter extends BaseAdapter {

	private List<Order> list;
	private Context context;
	//BitmapUtils bitmapUtils;
	private final static String TAG = "MyOrderListAdapter";

	String cash_on_delivery = "";
	 DecimalFormat df = new DecimalFormat("#########.##");

	public MyOrderListAdapter(List<Order> list, Context ctx) {
		this.list = list;
		this.context = ctx;
		cash_on_delivery = context
				.getString(R.string.order_ensure_pay_method_cash_on_delivery);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {

		ViewHolder viewHolder = null;
		if (convertView == null) {
			LayoutInflater inflater = ((Activity) context).getLayoutInflater();
			convertView = inflater.inflate(R.layout.orderlist_item, null);
			viewHolder = new ViewHolder();

			viewHolder.storeName = (TextView) convertView
					.findViewById(R.id.storeName);
			viewHolder.status = (TextView) convertView
					.findViewById(R.id.status);
			viewHolder.name = (TextView) convertView.findViewById(R.id.name);
			viewHolder.price = (TextView) convertView.findViewById(R.id.price);
			viewHolder.nums = (TextView) convertView.findViewById(R.id.nums);
			viewHolder.totalNums = (TextView) convertView
					.findViewById(R.id.totalNums);
			viewHolder.shipPrice = (TextView) convertView
					.findViewById(R.id.shipPrice);
			viewHolder.totalPrice = (TextView) convertView
					.findViewById(R.id.totalPrice);

			viewHolder.icon = (ImageView) convertView.findViewById(R.id.icon);
			viewHolder.close = (Button) convertView.findViewById(R.id.close);
			viewHolder.payContinue = (Button) convertView
					.findViewById(R.id.payContinue);
			viewHolder.createTime = (TextView) convertView
					.findViewById(R.id.createTime);
			
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		final Order order = list.get(position);
		
		UtilTools.processOrderStatus(viewHolder.close, viewHolder.payContinue, viewHolder.status, context, order);

		Date date = order.getCreatedAt();
		viewHolder.createTime.setText(context.getString(R.string.order_create_time)+DateFormat.format("yyyy-MM-dd HH:mm",
				date));
		
		StoreInfo tempStore = AntHomeApplication.storeInfos.get(order
				.getStoreId());
		if (tempStore != null) {
			viewHolder.storeName.setText(tempStore.getDescrip());
		}
		else{
			viewHolder.storeName.setText("");
		}
		List<OrderItem> orderItems = order.getOrderItems();
		if (orderItems.size() > 0) {
			String name = "";
			String imageUrl="";
			for (int i = 0; i < orderItems.size(); i++) {
				String temp = orderItems.get(i).getName();
				if (!TextUtils.isEmpty(temp)) {
					name += (temp + "+");
				}
				String url=orderItems.get(i).getPicUrl();
				if(TextUtils.isEmpty(imageUrl))
				{
					imageUrl=url;
				}

			}
			if (name.length() > 0)
				name = name.substring(0, name.length() - 1);
			viewHolder.name.setText(name);
			if(TextUtils.isEmpty(imageUrl))
			{
				viewHolder.icon.setImageResource(R.drawable.pictures_no);
			}
			else{
				//bitmapUtils.display(viewHolder.icon, imageUrl);
				ImageLoader.getInstance().displayImage(imageUrl, viewHolder.icon,AntHomeApplication.imageDisplayOptions);
			}
			
		}else{
			viewHolder.name.setText("");
			viewHolder.icon.setImageResource(R.drawable.pictures_no);
		}

		
		viewHolder.nums.setText(String.format(
				context.getString(R.string.orderdetail_quantity), ""
						+ order.getOrderItems().size()) );
		
		viewHolder.totalNums.setText(String.format(
				context.getString(R.string.button_order_totalnums), ""
						+ orderItems.size()));
		viewHolder.shipPrice.setText(String.format(
				context.getString(R.string.button_order_shipprice), "￥" + ""
						+ df.format(order.getDeliveryCost())));
		
		String totalCost=UtilTools.getTotalCostByOrderItems(orderItems);
		String totalDesc="￥--.--";
		String totalGoodsDesc="￥--.--";
		double tempTotal=Double.parseDouble(totalCost);
		if(tempTotal>0.0001)
		{
		    totalGoodsDesc="￥"+String.valueOf(totalCost); 
		    tempTotal+=order.getDeliveryCost();
		    tempTotal-=order.getFavorableCost();//减去优惠金额
		    totalDesc="￥"+df.format(tempTotal); 
		}else{
		    
		}
		viewHolder.totalPrice.setText(String.format(
				context.getString(R.string.button_order_totalprice), totalDesc));
		
		viewHolder.price.setText(totalGoodsDesc);

		final Button payContinue = viewHolder.payContinue;

		
		final Button close = viewHolder.close;
		
		UtilTools.processOrderButtonClick(close, payContinue, viewHolder.status, context, order, this);
		// 跳转到订单详情页面
        convertView.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                Intent intent = new Intent(context, OrderDetailActivity.class);
                Const.order=order;
                context.startActivity(intent);
            }
        });

		return convertView;
	}

	class ViewHolder {
		public TextView storeName;
		public TextView status;
		public ImageView icon;
		public TextView name;
		public TextView price;
		public TextView nums;
		public TextView createTime;
		public TextView totalNums;
		public TextView shipPrice;
		public TextView totalPrice;
		public Button close;
		public Button payContinue;
	}

}
